package com.yhny.lykj.yuhannongye.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.UserConfig;
import com.yhny.lykj.yuhannongye.model.UserInfoModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.LCAppUtils;
import com.yhny.lykj.yuhannongye.tools.Tools;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity {
    private EditText phoneEt;
    private EditText passwordEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        TextView topName = (TextView)findViewById(R.id.name_top_bar);
        topName.setText("登录");
        RelativeLayout left_group = (RelativeLayout)findViewById(R.id.left_group);
        left_group.setVisibility(View.GONE);
        phoneEt = (EditText)findViewById(R.id.phone);
        passwordEt = (EditText)findViewById(R.id.password);
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.refresh).setOnClickListener(this);
        findViewById(R.id.unpassword).setOnClickListener(this);
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                final UserInfoModel userInfoModel = (UserInfoModel)model;

                if (!isDataEmpty(userInfoModel)){
                    dismissProgressDialog();
                    if (userInfoModel.status==1){
                        UserConfig.setConfigUserId(userInfoModel.data.user_id);
                        UserConfig.setConfigUserName(userInfoModel.data.truename);
                        UserConfig.setConfigUserPhotoUrl(userInfoModel.data.userpic);
                        UserConfig.setConfigUserMobile(userInfoModel.data.usertel);
                        Intent intent = new Intent(this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        LSCToast.show(LoginActivity.this,userInfoModel.msg);
                    }
                }else{
                    dismissProgressDialog();
                    if (userInfoModel!=null&&!TextUtils.isEmpty(userInfoModel.msg)){
                        LSCToast.show(LoginActivity.this,userInfoModel.msg);
                    }else{
                        LSCToast.show(LoginActivity.this,"操作失败！");
                    }

                }
                break;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.refresh:{
                //注册
                Intent intent = new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.unpassword:{
                //找回密码
                Intent intent = new Intent(this,PasswordActivity.class);
                intent.putExtra(PASSWORD_ACTIVITY,UNKNOW_PASSWORD);
                startActivity(intent);
                break;
            }
            case R.id.sign_in_button:{
                //登录
                String phone = phoneEt.getText().toString();
                String password = passwordEt.getText().toString();
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输入手机号");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号");
                    return;
                }
                if (password.isEmpty()){
                    LSCToast.show(this,"请输入密码");
                    return;
                }
                if (password.length()<6&&password.length()>32){
                    LSCToast.show(this,"请输入6-32位密码");
                    return;
                }
                showProgressDialog(R.string.text_login_loading);
                String ip = LCAppUtils.getIp(LoginActivity.this);
                DataManager.getData(MODEL_PROCESS_0, NetHelper.User.login(phone,password,ip), this);
                break;
            }
        }
    }
}

